<?php

include_once APPLICATION_DIR . "/application/model/BOOKING_DETAIL.php";

class Booking_detail  extends DataObjects_BOOKING_DETAIL
{
	public function validateInput(){
        global $error_msg;
		return true;
		return $error_msg->isNoError();
    }
	
	public static function getbookingDetail($bookingID=0,$filter = array()){
		$booking = new Booking_detail();
		$query = "SELECT B.ID, BD.ID ID_BOOKING_DETAIL, BD.ID_TECHNICIAN, T.TITLE TECHNICIAN_NAME, BD.BOOKING_DATE, 
					BDS.ID_SERVICE_BOOKING, SB.TITLE SERVICE_TITLE, SB.TIME,SB.PRICE, 
					IF(B.ID_MEMBER > 0, M.FIRST_NAME,B.FIRST_NAME) U_FIRST_NAME,
					IF(B.ID_MEMBER > 0, M.LAST_NAME,B.LAST_NAME) U_LAST_NAME,
					IF(B.ID_MEMBER > 0, M.PHONE,B.PHONE) U_PHONE,
					IF(B.ID_MEMBER > 0, M.EMAIL,B.EMAIL) U_EMAIL,
                    B.STATUS,
					B.ID_MEMBER
				FROM BOOKING B
				INNER JOIN BOOKING_DETAIL BD ON BD.ID_BOOKING = B.ID
				INNER JOIN BOOKING_DETAIL_SERVICE BDS ON BDS.ID_BOOKING_DETAIL = BD.ID
				INNER JOIN SERVICE_BOOKING SB ON SB.ID = BDS.ID_SERVICE_BOOKING
				LEFT JOIN TECHNICIAN T ON T.ID = BD.ID_TECHNICIAN
				LEFT JOIN MEMBER M ON M.ID = B.ID_MEMBER 
                WHERE 1 =1 ".
				($bookingID>0?" AND B.ID = {$bookingID} ": " ").
                (isset($filter["ID_MEMBER"]) && $filter["ID_MEMBER"]>0?" AND B.ID_MEMBER = {$filter["ID_MEMBER"]} ": " ").
				(isset($filter["ID_SERVICE"]) && $filter["ID_SERVICE"]>0?" AND BDS.ID_SERVICE_BOOKING = {$filter["ID_SERVICE"]} ": " ").
                (isset($filter["ID_TECHNICIAN"]) && $filter["ID_TECHNICIAN"]>0?" AND BD.ID_TECHNICIAN = {$filter["ID_TECHNICIAN"]} ": " ").
                (isset($filter["STATUS"]) && $filter["STATUS"]>=0?" AND B.STATUS = {$filter["STATUS"]} ": " ").
				(isset($filter["START_DATE"]) && $filter["START_DATE"]!=""?	" AND BD.BOOKING_DATE > '{$filter["START_DATE"]}' ": " ").
				(isset($filter["END_DATE"]) && $filter["END_DATE"]!=""?	" AND DATE(BD.BOOKING_DATE) <= '{$filter["END_DATE"]}' ": " ").
				"ORDER BY B.ID,BD.ID, SB.TITLE ";
		$booking->query($query);
		$arrData = array();
		while($booking->fetch()){
			if(!isset($arrData[$booking->ID])){
				$u = array(
						"FIRST_NAME"=>$booking->U_FIRST_NAME,
						"LAST_NAME"=>$booking->U_LAST_NAME,
						"PHONE"=>$booking->U_PHONE,
						"EMAIL"=>$booking->U_EMAIL,
						"ID_MEMBER"=>$booking->ID_MEMBER,
					);
				$arrData[$booking->ID] = array("USER_INFO"=>$u,"STATUS"=>$booking->STATUS,"TDATA"=>array());
			}
			if(!isset($arrData[$booking->ID]["TDATA"][$booking->ID_BOOKING_DETAIL])){
				$arrData[$booking->ID]["TDATA"][$booking->ID_BOOKING_DETAIL] = array("TITLE" => $booking->TECHNICIAN_NAME,
																	"ID_TECHNICIAN" => $booking->ID_TECHNICIAN,
																	"BOOKING_DATE"=>$booking->BOOKING_DATE,
																		"SDATA" => array());
			}
			
			$arrData[$booking->ID]["TDATA"][$booking->ID_BOOKING_DETAIL]["SDATA"][$booking->ID_SERVICE_BOOKING] = array("SID" => $booking->ID_SERVICE_BOOKING,
																				"TITLE"=>$booking->SERVICE_TITLE."(".$booking->TIME."mins  - $".$booking->PRICE.")",
																				"TIME"=>$booking->TIME);
		}
		return $arrData;
	}
	
	public function update() {
		return parent::update();
	}

	public function delete() {
		return parent::delete();
	}
    public function status2Text($status){
        return status2Text($status);
    }
}
?>